<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\exception\HttpResponseException;


class Admin extends BaseController
{
    protected $sysName = 'admin';
    protected $admin;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $admin = session($this->sysName);

        if (!empty($admin)) {
            $life = config("session.life_time.{$this->sysName}");
            if ((int)$admin['session_time'] + (int)$life < time()) {
                $adminService = new \app\admin\service\Admin();
                $admin = $adminService->getUpdate($admin['admin_id']);
                if (!$admin) {
                    session($this->sysName, null);
                    cookie("{$this->sysName}_auto_login", null);
                    $this->redirectTo(url('/admin/login/index'));
                } else {
                    $admin['session_time'] = time();
                    session($this->sysName, $admin);
                }
            }
        }

        $this->admin = session($this->sysName);
    }

    public function redirectTo(...$args)
    {
        //此处 throw new HttpResponseException 这个异常一定要写
        throw new HttpResponseException(redirect(...$args));
    }
}

